package com.example.customview.bezier.water

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

class WaterView  @JvmOverloads constructor(context: Context,att:AttributeSet?=null,defaultStyle:Int = 0):View(context,att,defaultStyle) {
    private val mPaint = Paint()
    private val mPath = Path()
    private val waveLength  =2000
    init {
        mPaint.style = Paint.Style.FILL_AND_STROKE
        mPaint.color = Color.RED
        mPaint.textSize = 10f

        initAnimate()

    }
    private var mX = 0
    private fun initAnimate() {
        val ofInt = ValueAnimator.ofInt(0,waveLength)
        ofInt.addUpdateListener {
            mX = it.animatedValue as Int
            postInvalidate()
        }
        ofInt.duration = 3000
        ofInt.interpolator = LinearInterpolator()
       // ofInt.repeatMode = ValueAnimator.REVERSE
        ofInt.repeatCount = ValueAnimator.INFINITE
        ofInt.start()



    }

    override fun onDraw(canvas: Canvas) {

        val y = height/3
          mPath.reset()
        mPath.moveTo((-waveLength+mX).toFloat(), y.toFloat())
       // mPath.moveTo(0f, y.toFloat())
        val waterWidth = waveLength/2
        for (i in -waveLength until waveLength+width step 50) {
            mPath.rQuadTo((waterWidth/2).toFloat(), 200.toFloat(),waterWidth.toFloat(), 0f)
            mPath.rQuadTo((waterWidth/2).toFloat(), -200.toFloat(),waterWidth.toFloat(), 0f)
        }

        mPath.lineTo(width.toFloat(), height.toFloat())
        mPath.lineTo(0f, height.toFloat())
        mPath.close()
        canvas.drawPath(mPath,mPaint)



    }
}